import { couponApi } from "@/utils/api";
import moment from "moment";
interface IndexstateType {
  couponList: string[];
  couponListOrigin: string[];
  currentPage: number; //当前页数
  pageSize: number; //每页显示条目个数
  pageTotal: number; //数据总数
}
export const couponModule = {
  namespaced: true,
  state: {
    //优惠券数据
    // couponList: [
    //   {
    //     id: "1",
    //     name: "满300立减60元",
    //     mode: "领取",
    //     date: "05/31/00:00:00-06/20 11:59:59",
    //     use: "跨店满300-60",
    //     num: "158/300",
    //     grant: "平台",
    //     state: "进行中",
    //   },
    // ],
    couponListOrigin: [],
    couponList: [],
    currentPage: 1,
    pageSize: 10,
    pageTotal: 0,
  },
  mutations: {
    //添加优惠券
    addCoupon(state: any, payload: any) {
      const date1 = moment(payload.date1).format("MM/DD HH:mm:ss");
      const date2 = moment(payload.date2).format("MM/DD HH:mm:ss");
      const dates = `${date1}-${date2}`;
      const addCouponList: any = {
        id: new Date().getTime(),
        name: payload.name,
        mode: payload.type,
        date: dates,
        use: payload.name,
        num: payload.num,
        grant: payload.store,
        state: "进行中",
      };
      state.couponListOrigin.unshift(addCouponList);
      state.pageTotal = state.couponListOrigin.length;
      //计算当前页数的数据
      state.couponList = state.couponListOrigin.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },
    //修改优惠券
    change(state: any, row: any) {
      state.couponListOrigin.forEach((v: any) => {
        if (v.id === row.id) {
          v = row;
        }
        //计算当前页数的数据
        state.couponList = state.couponListOrigin.slice(
          (state.currentPage - 1) * state.pageSize,
          state.currentPage * state.pageSize
        );
      });
    },
    //查询优惠券
    checkCoupon(state: any, obj: any) {
      state.couponList = state.couponListOrigin?.filter((item: any) => {
        const keys = Object.keys(obj);
        return keys.every((key: string) => obj[key] === item[key] || !obj[key]);
      });
      state.pageTotal = state.couponListOrigin.length;
      //计算当前页数的数据，较其他更特殊的计算方式
      state.couponList = state.couponList.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },
    //删除优惠券
    removeCoupon(state: any, id: any) {
      state.couponListOrigin = state.couponListOrigin.filter((v: any) => {
        return v.id !== id;
      });
      state.pageTotal = state.couponListOrigin.length;
      //计算当前页数的数据
      state.couponList = state.couponListOrigin.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },
    //批量选择
    moreOver(state: any, id: any) {
      // row.focus = !row.focus;
      // console.log(state.couponList);
      state.couponListOrigin.forEach((v: any) => {
        if (v.id === id) {
          v.focus = !v.focus;
          console.log(v.focus);
        }
      });
    },
    //全选
    moreAll(state: any, all: any) {
      state.couponListOrigin.forEach((v: any) => {
        all.forEach((n: any) => {
          if (n.id === v.id) {
            v.focus = true;
          }
        });
      });
    },
    //批量终止
    moreOverBtn(state: any) {
      state.couponListOrigin.forEach((v: any) => {
        if (v.focus) {
          v.state = "已下架";
        }
      });
      //计算当前页数的数据
      state.couponList = state.couponListOrigin.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },
    //批量删除
    removeCoupon2(state: any) {
      state.couponListOrigin = state.couponListOrigin.filter((v: any) => {
        return !v.focus;
      });
      state.pageTotal = state.couponListOrigin.length;
      //计算当前页数的数据
      state.couponList = state.couponListOrigin.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },
    //数据请求
    getcouponstate(state: IndexstateType, payload: any) {
      state.couponListOrigin = payload.map((item: any) => {
        return {
          ...item,
          focus: false,
          change: false,
        };
      });

      state.pageTotal = state.couponListOrigin.length;
      state.currentPage = 1;
      //计算当前页数的数据
      state.couponList = state.couponListOrigin.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },

    //分页
    pageCurrentChange(state: IndexstateType, page: any) {
      state.currentPage = page;
      //计算当前页数的数据
      state.couponList = state.couponListOrigin.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },
    //给出当前数据
    pageSizeChange(state: IndexstateType, page: any) {
      state.pageSize = page;
      //计算当前页数的数据
      state.couponList = state.couponListOrigin.slice(
        (state.currentPage - 1) * state.pageSize,
        state.currentPage * state.pageSize
      );
    },
  },
  actions: {
    //数据请求
    async getcouponAsync({ commit }: any) {
      const res = await couponApi();

      commit("getcouponstate", res.result);
    },
  },
};

export default couponModule;
